﻿using Client.Constant;
using Client.Enum;
using Client.Framework.Event;
using Client.Framework.Message;
using Client.Framework.Socket;
using Client.Message.Request;
using Client.Message.Response;
using Client.Model;
using Client.View;
using Client.View.MenuView;
using MaterialDesignThemes.Wpf;
using Prism.Commands;
using Prism.Mvvm;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Client.ViewModel.MenuViewModel
{
    /// <summary>
    /// Author: Leil
    /// Description: MenuListViewModel
    /// CreateTime: 2020/5/28 22:53:22
    /// </summary>
    public class MenuListViewModel : BindableBase
    {
        public MenuListViewModel()
        {
            MsgEventSubscribe();
            Init();
        }

        /// <summary>
        /// 页面初始化
        /// </summary>
        private void Init()
        {
            AddMenuCommand = new DelegateCommand(AddMenu);
            RefreshMenuListCommand = new DelegateCommand(RefreshMenuList);
            PrePageCommand = new DelegateCommand(PrePage);
            NextPageCommand = new DelegateCommand(NextPage);
            EditMenuCommand = new DelegateCommand<ViewMenu>(EditMenu);
            DeleteMenuCommand = new DelegateCommand<ViewMenu>(DeleteMenu);
            MenuEditWindow = new MenuEdit();

            SearchCommand = new DelegateCommand(Search);
            ResetCommand = new DelegateCommand(Reset);

            HasPrePage = false;
            HasNextPage = false;

            InitMenus();
        }

        public void InitMenus()
        {
            GetTopMenus();
            GetMenus();
        }

        /// <summary>
        /// 消息事件订阅
        /// </summary>
        private void MsgEventSubscribe()
        {
            MessageEventPublisher.SubscribeEvent(MessageId.MENU_LIST, MenusResp);
            MessageEventPublisher.SubscribeEvent(MessageId.RESPONSE_TOP_MENUS, TopMenusResponse);
            MessageEventPublisher.SubscribeEvent(MessageId.OPT_RESP, ReceiveOptMsg);
        }


        public List<MainMenu> TopMenus { get; set; }

        private List<ViewMenu> menus;

        public List<ViewMenu> Menus
        {
            get { return menus; }
            set
            {
                menus = value;
                RaisePropertyChanged("Menus");
            }
        }



        /// <summary>
        /// 新增编辑页面
        /// </summary>
        public MenuEdit MenuEditWindow { get; private set; }

        /// <summary>
        /// 当前页码
        /// </summary>
        private int currentPage = 1;

        public int CurrentPage
        {
            get { return currentPage; }
            set { currentPage = value; RaisePropertyChanged("CurrentPage"); }
        }

        /// <summary>
        /// 每页条数
        /// </summary>
        private readonly int LIMIT = 10;

        /// <summary>
        /// 是否有上一页
        /// </summary>
        private bool hasPrePage;

        public bool HasPrePage
        {
            get { return hasPrePage; }
            set
            {
                hasPrePage = value;
                RaisePropertyChanged("HasPrePage");
            }
        }



        /// <summary>
        /// 是否有下一页
        /// </summary>
        private bool hasNextPage;

        public bool HasNextPage
        {
            get { return hasNextPage; }
            set
            {
                hasNextPage = value;
                RaisePropertyChanged("HasNextPage");
            }
        }


        /// <summary>
        /// 新增角色
        /// </summary>
        public DelegateCommand AddMenuCommand { get; private set; }
        public void AddMenu()
        {
            MenuEditWindow.BindParentContextAndEntity(this);
            MenuEditWindow.ShowDialog();
        }

        /// <summary>
        /// 刷新角色列表
        /// </summary>
        public DelegateCommand RefreshMenuListCommand { get; private set; }
        public void RefreshMenuList()
        {
            GetMenus();
        }

        /// <summary>
        /// 上一页
        /// </summary>
        public DelegateCommand PrePageCommand { get; private set; }
        public void PrePage()
        {
            if (hasPrePage)
            {
                CurrentPage--;
                GetMenus();
            }
        }

        /// <summary>
        /// 下一页
        /// </summary>
        public DelegateCommand NextPageCommand { get; private set; }
        public void NextPage()
        {
            if (hasNextPage)
            {
                CurrentPage++;
                GetMenus();
            }
        }

        /// <summary>
        /// 编辑角色信息
        /// </summary>
        public DelegateCommand<ViewMenu> EditMenuCommand { get; private set; }
        public void EditMenu(ViewMenu menu)
        {
            MenuEditWindow.BindParentContextAndEntity(this, menu);
            MenuEditWindow.ShowDialog();
        }

        /// <summary>
        /// 删除角色信息
        /// </summary>
        public DelegateCommand<ViewMenu> DeleteMenuCommand { get; private set; }
        private int deleteMenuId;
        public void DeleteMenu(ViewMenu menu)
        {
            this.deleteMenuId = menu.ID;
            CancelConfirmTip tip = new CancelConfirmTip()
            {
                Message = { Text = $"确定删除菜单{menu.MenuName}吗？" }
            };
            DialogHost.Show(tip, "MainPanel", CloseTip);
        }

        public void CloseTip(object sender, DialogClosingEventArgs eventArgs)
        {
            if (string.Equals(eventArgs.Parameter, "Cancel")) return;
            DeleteMenuReq msg = new DeleteMenuReq(MessageId.DELETE_MENU)
            {
                ID = deleteMenuId
            };
            ApplicationClient.Instance().Send(msg);
        }




        /// <summary>
        /// 获取菜单列表
        /// </summary>
        public void GetMenus()
        {
            MenuListReq msg = new MenuListReq(MessageId.REQUEST_MENU_LIST)
            {
                Page = CurrentPage,
                Limit = LIMIT,
                Condition = Condition
            };
            ApplicationClient.Instance().Send(msg);
        }


        /// <summary>
        /// 列表信息返回
        /// </summary>
        /// <param name="message">返回的消息</param>
        private void MenusResp(ResponseBaseMessage message)
        {
            if (message is MenuListResp msg)
            {
                Menus = msg.Menus;
                Menus.ForEach(menu =>
                {
                    menu.ParentMenuName = TopMenus.Find(top =>
                    {
                        return top.ID == menu.ParentId;
                    }).Name;
                });
                HasPrePage = CurrentPage > 1;
                HasNextPage = CurrentPage < (int)Math.Ceiling((double)msg.Count / LIMIT);
            }
        }




        public void GetTopMenus()
        {
            TopMenusReq msg = new TopMenusReq(MessageId.REQUEST_TOP_MENUS);
            ApplicationClient.Instance().Send(msg);
        }


        private void TopMenusResponse(ResponseBaseMessage message)
        {
            if (message is TopMenusResp msg)
            {
                TopMenus = msg.MainMenus;
                MenuEditWindow.MenuEditViewModel.TopMenus = msg.MainMenus;
            }
        }


        /// <summary>
        /// 操作回复消息接收
        /// </summary>
        /// <param name="message">回复消息</param>
        private void ReceiveOptMsg(ResponseBaseMessage message)
        {
            if (message is OperationResp msg)
            {
                if (msg.Code == 200 && msg.OptType == (int)OptType.DELETE_MENU)
                {
                    GetMenus();
                }
            }
        }


        private ViewMenu condition = new ViewMenu() { Status = 0, ParentId = 9999 };

        public ViewMenu Condition
        {
            get { return condition; }
            set
            {
                condition = value;
                RaisePropertyChanged("Condition");
            }
        }

        public DelegateCommand SearchCommand { get; set; }
        private void Search()
        {
            if (SelectedParentMenu != null)
            {
                Condition.ParentId = selectedParentMenu.ID;
            }
            GetMenus();
        }

        public DelegateCommand ResetCommand { get; set; }
        private void Reset()
        {
            Condition.MenuName = string.Empty;
            Condition.ParentId = 9999;
            Condition.Status = 0;
            SelectedParentMenu = null;
            GetMenus();
        }


        private MainMenu selectedParentMenu;

        public MainMenu SelectedParentMenu
        {
            get { return selectedParentMenu; }
            set
            {
                selectedParentMenu = value;
                RaisePropertyChanged("SelectedParentMenu");
            }
        }


    }
}
